function $(ElementId) {
  return document.getElementById(ElementId);
}

function checkUser() {
  var user = $("user").value;
  var userId = $("user_prompt");
  if (user.length < 4 || user.length > 12) {
    userId.innerHTML = "用户名长度为在4-12字符";
    return false;
  }
  var usern = /^[a-zA-Z0-9_]{1,}$/;
  if (!usern.test(user)) {
    userId.innerHTML = "用户名只能由字母、数字和下划线组成";
    return false;
  }
  userId.innerHTML = "用户名可用";
  return true;
}
function checkPwd() {
  var pwd = $("pwd").value;
  var pwdId = $("pwd_prompt");
  pwdId.innerHTML = "";
  if (pwd.length < 4 || pwd.length > 12) {
    pwdId.innerHTML = "密码长度在4-12之间";
    return false;
  }
  return true;
}
function checkRepwd() {
  var repwd = $("repwd").value;
  var pwd = $("pwd").value;
  var repwdId = $("repwd_prompt");
  repwdId.innerHTML = "";
  pwdBoolean = checkPwd();
  console.log(pwdBoolean);
  if (!pwdBoolean) {
    repwdId.innerHTML = "请先输入正确密码";
    return false;
  }
  if (pwd != repwd) {
    repwdId.innerHTML = "两次输入的密码不一致";
    return false;
  }
  repwdId.innerHTML = "两次输入的密码一致";
  return true;
}
function checkSex() {
  var sexId = $("sexId");
  var checked = false;
  var gender = document.getElementsByName("sex");
  for (var i = 0; i < gender.length; i++) {
    if (gender[i].checked) {
      checked = true;
      return true;
      break;
    }
  }
  if (checked == false) {
    sexId.innerHTML = "必须选一个";
  }
}
function checkEmail() {
  // checkSex();
  var email = $("email").value;
  var email_prompt = $("email_prompt");
  email_prompt.innerHTML = "";
  var index = email.indexOf("@", 1);
  if (index == -1) {
    email_prompt.innerHTML = "输入的邮箱格式中应包含@符号";
    return false;
  }
  if (email.indexOf(".", index) == -1) {
    email_prompt.innerHTML = "输入的邮箱格式中应包含.符号且在@符号后面";
    return false;
  }
  email_prompt.innerHTML = "邮箱格式正确";
  return true;
}
function checkYear() {
  var year = $("year").value;
  var year_prompt = $("year_prompt");
  year_prompt.innerHTML = year + "年";
}
function checkMonth() {
  var month = $("month").value;
  var month_prompt = $("month_prompt");
  month_prompt.innerHTML = month + "月";
}
function checkDay() {
  var day = $("day").value;
  var day_prompt = $("day_prompt");
  day_prompt.innerHTML = day + "日";
}

function check() {
  if (checkUser() && checkPwd() && checkRepwd() && checkEmail() && checkSex()) {
    //所有函数返回true时提交表单
    return true;
  } else {
    return false;
  }
}
